﻿using System;
using System.Collections.Generic;
using YiYiTong.Rocket.Business;
using YiYiTong.Rocket.Model;
using YiYiTong.Rocket.Helper;

namespace YiYiTong.Rocket.Api.Biz
{
    public class SmsService : ApiService
    {
        /// <summary>
        /// 发送手机短信
        /// </summary>
        public void Send(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            string mobile = dic.GetValue<string>("mobile");
            //短信类型 1 登录短信
            int sendType = dic.GetValue<int>("sendType", 0);

            if (string.IsNullOrEmpty(mobile) || sendType == 0)
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            //检查手机号是否合法
            if (!Utils.CheckMobile(mobile))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "手机号码错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            //验证短信类型
            if (!EnumHelper.IsDefined<SendType>(sendType))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "短信类型错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            //开始发送短信
            if (SmsBiz.Instance.SendMessage(mobile, sendType))
            {
                apiResponse.retCode = ResultEnum.Success;
            }
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = "短信发送失败";
            }
            apiContext.apiHttpResp = apiResponse;
        }
    }
}